home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2825 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  82 lines

  1. Path: news.mindlink.net!news
  2. From: genew@mindlink.bc.ca (Gene Wirchenko)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sscanf problems
  5. Date: Wed, 24 Jan 1996 05:43:38 GMT
  6. Organization: MIND LINK! - British Columbia, Canada
  7. Message-ID: <4e4h0j$af7@fountain.mindlink.net>
  8. References: <4e4c2v$j2g@mathserv.mps.ohio-state.edu>
  9. NNTP-Posting-Host: line257.nwm.mindlink.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Chris Mongold <cmongold@magnus.acs.ohio-state.edu> wrote:
  13.  
  14. >Hello,
  15. >    I'm sorry if this is an inappropriate topic, but I've tried
  16. >everything else.  I can't seem to get sscanf to to separate a string
  17. >into various variable types.  Here is an example:
  18.  
  19. >#include <stdio.h>
  20.  
  21. >void main()
  22.    ^
  23.      This is a no-no.  Per the Standard, main() returns int.
  24.  
  25. >{
  26. >char input[20], crap[17], segment[6], seg_len[3], begin[3], load[3];
  27. >int type;
  28. >FILE *fp;
  29.  
  30. >printf("File: ");
  31. >gets(input);
  32.  
  33.      Buffer can be overflowed.  You might want to use fgets().
  34.  
  35. >fp = fopen(input, "r");
  36.  
  37.      You don't check if the file could be opened.  This may be the
  38. problem.  Add:
  39.  
  40.           if (fp==NULL)
  41.              {
  42.              printf("Input file could not be opened.\n");
  43.              exit(EXIT_FAILURE);
  44.              }
  45.  
  46. >while(!feof(fp))
  47. >{
  48. >fgets(crap, 17, fp);
  49. >sscanf(crap, "%d%3s%6s%3s%3s", type, begin, segment, seg_len, load);
  50. >printf("%d %3s %6s %3s %3s", type, begin, segment, seg_len, load);
  51.                                 ^
  52.      type is never assigned a value.
  53.  
  54. >}
  55.  
  56.      Since main() returns a value, return one.  Add:
  57.           return 0;
  58.  
  59. >}
  60. >No matter what I do, it always 'bus errors' when I sscanf.  I've tried
  61. >it several different ways, including making crap larger, but to
  62. >no avail.  If anyone can help me, I would greatly appreciate it.
  63.  
  64.      I wonder if your data is in the wrong format.  Include a copy of
  65. it.
  66.      I suspect you'll need to copy the chars to their dest.
  67.  
  68. >Please respond to cmongold@magnus.acs.ohio-state.edu 
  69.  
  70. >Thanks,
  71.  
  72. >Chris 
  73.  
  74. Sincerely,
  75.  
  76. Gene Wirchenko
  77.  
  78. C Pronunciation Guide:
  79.      y=x++;     "wye equals ex plus plus semicolon"
  80.      x=x++;     "ex equals ex doublecross semicolon"
  81.  
  82.